<template>
  <!-- :model和下面数据绑定，rules设置校验规则，ref相当于表单的名字 -->
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="学号" prop="sid">
      <el-input v-model="ruleForm.sid" readonly="true"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="ruleForm.name" readonly="true"></el-input>
    </el-form-item>
    <el-form-item label="年级" prop="year">
      <el-input v-model="ruleForm.year" readonly="true"></el-input>
    </el-form-item>
    <el-form-item label="专业" prop="major">
      <el-input v-model="ruleForm.major" readonly="true"></el-input>
    </el-form-item>
    <el-form-item label="学院" prop="college">
      <el-input v-model="ruleForm.college" readonly="true"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="ruleForm.username" readonly="true"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="ruleForm.password" autocomplete="off" show-password="show-password"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
      <el-button @click="resetForm()">重置</el-button>
      <el-button @click="back()">返回</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  data() {
    return {
      six: '',
      ruleForm: {
        sid: '',
        name: '',
        year: '',
        major: '',
        college: '',
        username: '',
        password: '',
      },
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  created(){
    let list = decodeURIComponent(this.$route.query.row);
    this.ruleForm = JSON.parse(list);
  },
  methods: {
    back(){
      this.$router.back();
    },
    submitForm(formName) {
      const _this = this;
      this.$refs[formName].validate((valid) => {
        // 如果全部校验好了，valid为true
        if (valid) {
          this.$confirm('确认修改信息?', '修改', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            //用这种方式传变量
            axios.post('http://localhost:8000/std/updateStudent',this.ruleForm).then(function (resp){
              if(resp.data == "success"){
                _this.$message({
                  type: 'success',
                  message: '修改成功!'
                });
                _this.$router.back();
              }
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '取消修改'
            });
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm() {
      let list = decodeURIComponent(this.$route.query.row);
      this.ruleForm = JSON.parse(list);
    }
  }
}
</script>
